﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_ModelFieldDAL : IDcms_ModelFieldDAL
    {
        public Dcms_ModelFieldModel AddModel(Dcms_ModelFieldModel model)
        {
            string sQLString = "insert into Dcms_ModelField(mf_FieldName,mf_FieldLabel,mf_DisplayFlag,mf_Model,mf_ValidateType,mf_ValidateField,mf_RegexErrorMsg,mf_MustField,mf_Order,mf_FieldInfo,mf_FieldHelpInfo,mf_FieldDefaultValue,mf_RequiredField,mf_ModelKeyId) values(@mf_FieldName,@mf_FieldLabel,@mf_DisplayFlag,@mf_Model,@mf_ValidateType,@mf_ValidateField,@mf_RegexErrorMsg,@mf_MustField,@mf_Order,@mf_FieldInfo,@mf_FieldHelpInfo,@mf_FieldDefaultValue,@mf_RequiredField,@mf_ModelKeyId) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@mf_FieldName", SqlDbType.VarChar), new SqlParameter("@mf_FieldLabel", SqlDbType.VarChar), new SqlParameter("@mf_DisplayFlag", SqlDbType.SmallInt), new SqlParameter("@mf_Model", SqlDbType.VarChar), new SqlParameter("@mf_ValidateType", SqlDbType.SmallInt), new SqlParameter("@mf_ValidateField", SqlDbType.VarChar), new SqlParameter("@mf_RegexErrorMsg", SqlDbType.VarChar), new SqlParameter("@mf_MustField", SqlDbType.SmallInt), new SqlParameter("@mf_Order", SqlDbType.SmallInt), new SqlParameter("@mf_FieldInfo", SqlDbType.Text), new SqlParameter("@mf_FieldHelpInfo", SqlDbType.VarChar), new SqlParameter("@mf_FieldDefaultValue", SqlDbType.VarChar), new SqlParameter("@mf_RequiredField", SqlDbType.SmallInt), new SqlParameter("@mf_ModelKeyId", SqlDbType.SmallInt) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.mf_FieldName;
            cmdParms[2].Value = model.mf_FieldLabel;
            cmdParms[3].Value = model.mf_DisplayFlag;
            cmdParms[4].Value = model.mf_Model;
            cmdParms[5].Value = model.mf_ValidateType;
            cmdParms[6].Value = model.mf_ValidateField;
            cmdParms[7].Value = model.mf_RegexErrorMsg;
            cmdParms[8].Value = model.mf_MustField;
            cmdParms[9].Value = model.mf_Order;
            cmdParms[10].Value = model.mf_FieldInfo;
            cmdParms[11].Value = model.mf_FieldHelpInfo;
            cmdParms[12].Value = model.mf_FieldDefaultValue;
            cmdParms[13].Value = model.mf_RequiredField;
            cmdParms[14].Value = model.mf_ModelKeyId;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.mf_FieldID = (int) cmdParms[0].Value;
            return model;
        }

        public bool CopyDefaultModelByModel(string strModel, int intModelKeyId)
        {
            string sQLString = "insert into Dcms_ModelField (mf_FieldName,mf_FieldLabel,mf_DisplayFlag,mf_Model,mf_ValidateType,mf_ValidateField,mf_RegexErrorMsg,mf_MustField,mf_Order,mf_FieldInfo,mf_FieldHelpInfo,mf_FieldDefaultValue,mf_RequiredField,mf_ModelKeyId) select mf_FieldName,mf_FieldLabel,mf_DisplayFlag,mf_Model,mf_ValidateType,mf_ValidateField,mf_RegexErrorMsg,mf_MustField,mf_Order,mf_FieldInfo,mf_FieldHelpInfo,mf_FieldDefaultValue,mf_RequiredField, @mf_ModelKeyId from Dcms_ModelFieldTemp where mf_Model=@mf_Model";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@mf_Model", SqlDbType.VarChar), new SqlParameter("@mf_ModelKeyId", SqlDbType.SmallInt) };
            cmdParms[0].Value = strModel;
            cmdParms[1].Value = intModelKeyId;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return true;
        }

        public IList<Dcms_ModelFieldModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_ModelFieldModel> list = new List<Dcms_ModelFieldModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_ModelFieldModel item = new Dcms_ModelFieldModel();
                if (ds.Tables[0].Rows[i]["mf_FieldID"].ToString() != "") item.mf_FieldID = int.Parse(ds.Tables[0].Rows[i]["mf_FieldID"].ToString());
                item.mf_FieldName = ds.Tables[0].Rows[i]["mf_FieldName"].ToString();
                item.mf_FieldLabel = ds.Tables[0].Rows[i]["mf_FieldLabel"].ToString();
                if (ds.Tables[0].Rows[i]["mf_DisplayFlag"].ToString() != "") item.mf_DisplayFlag = int.Parse(ds.Tables[0].Rows[i]["mf_DisplayFlag"].ToString());
                item.mf_Model = ds.Tables[0].Rows[i]["mf_Model"].ToString();
                if (ds.Tables[0].Rows[i]["mf_ValidateType"].ToString() != "") item.mf_ValidateType = int.Parse(ds.Tables[0].Rows[i]["mf_ValidateType"].ToString());
                item.mf_ValidateField = ds.Tables[0].Rows[i]["mf_ValidateField"].ToString();
                item.mf_RegexErrorMsg = ds.Tables[0].Rows[i]["mf_RegexErrorMsg"].ToString();
                if (ds.Tables[0].Rows[i]["mf_MustField"].ToString() != "") item.mf_MustField = int.Parse(ds.Tables[0].Rows[i]["mf_MustField"].ToString());
                if (ds.Tables[0].Rows[i]["mf_Order"].ToString() != "") item.mf_Order = int.Parse(ds.Tables[0].Rows[i]["mf_Order"].ToString());
                item.mf_FieldInfo = ds.Tables[0].Rows[i]["mf_FieldInfo"].ToString();
                item.mf_FieldHelpInfo = ds.Tables[0].Rows[i]["mf_FieldHelpInfo"].ToString();
                item.mf_FieldDefaultValue = ds.Tables[0].Rows[i]["mf_FieldDefaultValue"].ToString();
                if (ds.Tables[0].Rows[i]["mf_RequiredField"].ToString() != "") item.mf_RequiredField = int.Parse(ds.Tables[0].Rows[i]["mf_RequiredField"].ToString());
                if (ds.Tables[0].Rows[i]["mf_ModelKeyId"].ToString() != "") item.mf_ModelKeyId = int.Parse(ds.Tables[0].Rows[i]["mf_ModelKeyId"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_ModelField where mf_FieldID=@mf_FieldID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("mf_FieldID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public void DeleteModelByModelAndModelKeyID(string mf_Model, int mf_ModelKeyID)
        {
            string sQLString = "delete from Dcms_ModelField where mf_Model=@mf_Model and mf_ModelKeyId=@mf_ModelKeyId";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@mf_Model", SqlDbType.VarChar), new SqlParameter("@mf_ModelKeyId", SqlDbType.SmallInt) };
            cmdParms[0].Value = mf_Model;
            cmdParms[1].Value = mf_ModelKeyID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public Dcms_ModelFieldModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_ModelField where mf_FieldID=@mf_FieldID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@mf_FieldID", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_ModelFieldModel model = new Dcms_ModelFieldModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_ModelFieldModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelKeyByModel(string strModel)
        {
            string sQLString = "select distinct mf_ModelKeyId from Dcms_ModelField where mf_Model = @mf_Model order by mf_ModelKeyId";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@mf_Model", SqlDbType.VarChar) };
            cmdParms[0].Value = strModel;
            return SqlHelper.CreateDataSet(sQLString, cmdParms);
        }

        public IList<Dcms_ModelFieldModel> GetModelList()
        {
            string sql = "select * from Dcms_ModelField";
            return this.DataSetToList(SqlHelper.CreateDataSet(sql));
        }

        public IList<Dcms_ModelFieldModel> GetModelListByModelAndModelKeyID(string mf_Model, int mf_ModelKeyID)
        {
            string sQLString = "select * from Dcms_ModelField where mf_Model=@mf_Model and mf_ModelKeyId=@mf_ModelKeyId";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@mf_Model", SqlDbType.VarChar), new SqlParameter("@mf_ModelKeyId", SqlDbType.SmallInt) };
            cmdParms[0].Value = mf_Model;
            cmdParms[1].Value = mf_ModelKeyID;
            return this.DataSetToList(SqlHelper.CreateDataSet(sQLString, cmdParms));
        }

        public DataSet GetModels()
        {
            string sql = "SELECT distinct Dcms_Permission.Permission_Id, Dcms_Permission.Permission_Name FROM Dcms_ModelField INNER JOIN Dcms_Permission ON (Dcms_ModelField.mf_ModelKeyId = Dcms_Permission.Permission_ModelKeyID) AND (Dcms_ModelField.mf_Model = Dcms_Permission.Permission_Flag)";
            return SqlHelper.CreateDataSet(sql);
        }

        public IList<Dcms_ModelFieldModel> GetShowModelListByModelAndModelKeyID(string mf_Model, int mf_ModelKeyID)
        {
            string sQLString = "select * from Dcms_ModelField where mf_Model=@mf_Model AND mf_ModelKeyId=@mf_ModelKeyId AND mf_DisplayFlag = 1 order by mf_Order";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@mf_Model", SqlDbType.VarChar), new SqlParameter("@mf_ModelKeyId", SqlDbType.SmallInt) };
            cmdParms[0].Value = mf_Model;
            cmdParms[1].Value = mf_ModelKeyID;
            return this.DataSetToList(SqlHelper.CreateDataSet(sQLString, cmdParms));
        }

        public Dcms_ModelFieldModel Update(Dcms_ModelFieldModel model)
        {
            string sQLString = "update Dcms_ModelField set mf_FieldName=@mf_FieldName,mf_FieldLabel=@mf_FieldLabel,mf_DisplayFlag=@mf_DisplayFlag,mf_Model=@mf_Model,mf_ValidateType=@mf_ValidateType,mf_ValidateField=@mf_ValidateField,mf_RegexErrorMsg=@mf_RegexErrorMsg,mf_MustField=@mf_MustField,mf_Order=@mf_Order,mf_FieldInfo=@mf_FieldInfo,mf_FieldHelpInfo=@mf_FieldHelpInfo,mf_FieldDefaultValue=@mf_FieldDefaultValue,mf_RequiredField=@mf_RequiredField,mf_ModelKeyId=@mf_ModelKeyId where mf_FieldID=@mf_FieldID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@mf_FieldName", SqlDbType.VarChar), new SqlParameter("@mf_FieldLabel", SqlDbType.VarChar), new SqlParameter("@mf_DisplayFlag", SqlDbType.SmallInt), new SqlParameter("@mf_Model", SqlDbType.VarChar), new SqlParameter("@mf_ValidateType", SqlDbType.SmallInt), new SqlParameter("@mf_ValidateField", SqlDbType.VarChar), new SqlParameter("@mf_RegexErrorMsg", SqlDbType.VarChar), new SqlParameter("@mf_MustField", SqlDbType.SmallInt), new SqlParameter("@mf_Order", SqlDbType.SmallInt), new SqlParameter("@mf_FieldInfo", SqlDbType.Text), new SqlParameter("@mf_FieldHelpInfo", SqlDbType.VarChar), new SqlParameter("@mf_FieldDefaultValue", SqlDbType.VarChar), new SqlParameter("@mf_RequiredField", SqlDbType.SmallInt), new SqlParameter("@mf_ModelKeyId", SqlDbType.SmallInt), new SqlParameter("@mf_FieldID", SqlDbType.Int) };
            cmdParms[0].Value = model.mf_FieldName;
            cmdParms[1].Value = model.mf_FieldLabel;
            cmdParms[2].Value = model.mf_DisplayFlag;
            cmdParms[3].Value = model.mf_Model;
            cmdParms[4].Value = model.mf_ValidateType;
            cmdParms[5].Value = model.mf_ValidateField;
            cmdParms[6].Value = model.mf_RegexErrorMsg;
            cmdParms[7].Value = model.mf_MustField;
            cmdParms[8].Value = model.mf_Order;
            cmdParms[9].Value = model.mf_FieldInfo;
            cmdParms[10].Value = model.mf_FieldHelpInfo;
            cmdParms[11].Value = model.mf_FieldDefaultValue;
            cmdParms[12].Value = model.mf_RequiredField;
            cmdParms[13].Value = model.mf_ModelKeyId;
            cmdParms[14].Value = model.mf_FieldID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

